package com.forum.impl.talk;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.entity.ApiResult;
import com.common.entity.PageEntity;
import com.common.utils.LocalContext;
import com.common.vo.LatestPosts;
import com.common.vo.TalkDetailVo;
import com.forum.mapper.forum.ForumUserMapper;
import com.forum.mapper.talk.TalkListMapper;
import com.forum.mapper.talk.TalkPostsMapper;
import com.forum.mapper.talk.TalkPostsUserMapper;
import com.forum.param.TalkQueryParam;
import com.forum.pojo.forum.ForumUser;
import com.forum.pojo.talk.TalkList;
import com.forum.pojo.talk.TalkPosts;
import com.forum.service.talk.TalkListService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Tsing
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TalkListServiceImpl extends ServiceImpl<TalkListMapper, TalkList> implements TalkListService {
    private final TalkListMapper talkListMapper;
    private final TalkPostsMapper talkPostsMapper;
    private final ForumUserMapper forumUserMapper;
    private final TalkPostsUserMapper talkPostsUserMapper;

    @Override
    public ApiResult<String> createTalk(TalkList talkList) {
        Integer currentUserId = LocalContext.getCurrentUserId();
        talkList.setManagerId(currentUserId);
        talkListMapper.insert(talkList);
        return ApiResult.success();
    }

    @Override
    public ApiResult<Map<String, Object>> getTalks(TalkQueryParam param) {
        Page<TalkList> page = new Page<>();
        page.setCurrent(param.getPage()).setSize(param.getSize());
        LambdaQueryWrapper<TalkList> queryWrapper = new LambdaQueryWrapper<>();
        Map<String,Object> map = new HashMap<>(16);
        //不按照热度查询
        if(param.getHot()==0L){
            queryWrapper.like(!Objects.isNull(param.getKeyword()),TalkList::getTalkName,param.getKeyword())
                    .or()
                    .like(!Objects.isNull(param.getKeyword()),TalkList::getSummary,param.getKeyword());
            Page<TalkList> talkListPage = talkListMapper.selectPage(page, queryWrapper);
            map.put("total",talkListPage.getTotal());
            map.put("list",talkListPage.getRecords());
        }
        //按照热度查询
        else{
            //获取帖子数量最多的前十个论坛
            List<TalkList> hoTalk = talkListMapper.getHoTalk();
            map.put("total",10);
            map.put("list",hoTalk);

        }
            return ApiResult.success(map);

    }

    @Override
    public ApiResult<Map<String, Object>> getTalksByCurrentUser(PageEntity page) {
        Integer currentUserId = LocalContext.getCurrentUserId();
        LambdaQueryWrapper<TalkList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TalkList::getManagerId,currentUserId);

        Page<TalkList> pageParam = new Page<>();
        pageParam.setCurrent(page.getPage()).setSize(page.getSize());

        Page<TalkList> talkListPage = talkListMapper.selectPage(pageParam, queryWrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("total",talkListPage.getTotal());
        map.put("list",talkListPage.getRecords());
        return ApiResult.success(map);
    }

    @Override
    public ApiResult<Map<String,Object>> getLatestTalks(PageEntity pageEntity) {
      LambdaQueryWrapper<TalkPosts> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.orderBy(true,false,TalkPosts::getCreateTime);
      Page<TalkPosts> pageParam = new Page<>();
      pageParam.setCurrent(pageEntity.getPage()).setSize(pageEntity.getSize());
        Page<TalkPosts> page = talkPostsMapper.selectPage(pageParam, queryWrapper);

        List<TalkPosts> records = page.getRecords();
        List<LatestPosts> latestPostsList = records.stream().map(item -> {
            LatestPosts latestPosts = new LatestPosts();
            BeanUtils.copyProperties(item,latestPosts);
            LambdaQueryWrapper<ForumUser> userWrapper = new LambdaQueryWrapper<>();
            userWrapper.eq(ForumUser::getId,item.getPublisherId());
            ForumUser forumUser = forumUserMapper.selectOne(userWrapper);
            latestPosts.setAvatar(forumUser.getAvatar());
            latestPosts.setNickname(forumUser.getNickname());

            LambdaQueryWrapper<TalkList> talkWrapper = new LambdaQueryWrapper<>();
            talkWrapper.eq(TalkList::getId,item.getTalkId());
            TalkList talkList = talkListMapper.selectOne(talkWrapper);
            latestPosts.setTalkName(talkList.getTalkName());


            return latestPosts;
        }).collect(Collectors.toList());


        Map<String,Object> map = new HashMap<>(16);


        map.put("total",page.getTotal());
        map.put("list",latestPostsList);
        return ApiResult.success(map);
    }

    @Override
    public ApiResult<?> getTalkById(Long talkId) {
        TalkDetailVo talkDetailVo = new TalkDetailVo();
        TalkList talkList = talkListMapper.selectById(talkId);
        BeanUtils.copyProperties(talkList,talkDetailVo);
        //获取该论坛的帖子总数
        LambdaQueryWrapper<TalkPosts> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TalkPosts::getTalkId,talkId);
        Integer count = talkPostsMapper.selectCount(queryWrapper);
        talkDetailVo.setCountPosts(count);

        return ApiResult.success(talkDetailVo);
    }

    @Override
    public ApiResult<?> getHotPostsBtTalk(Long talkId) {
        LambdaQueryWrapper<TalkPosts> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TalkPosts::getTalkId,talkId).last("limit 5");
        List<TalkPosts> talkPosts = talkPostsMapper.selectList(queryWrapper);
        return ApiResult.success(talkPosts);
    }
}
